package com.sss.portalrimac.web.registro.validator;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.sss.portalrimac.web.shared.constant.MessageCodeConstant;

public abstract class AbstractRegistroValidator implements Validator
{
	protected boolean esDNIValido(String dni)
	{
		if (StringUtils.isBlank(dni) || dni.length() != 8 || !StringUtils.isAlphanumeric(dni))
		{
			return false;
		}
		return true;
	}

	protected void rechazarValor(Errors errors, String field, String errorCode)
	{
		rechazarValor(errors, field, errorCode, null);
	}

	protected void rechazarValor(Errors errors, String field, String errorCode, String errorArg)
	{
		String[] args = errorArg != null ? new String[] { errorArg } : null;
		errors.rejectValue(field, errorCode, args, MessageCodeConstant.MSJ_ERROR_DEFAULT);
	}

	protected void validarRequerido(Errors errors, String field, String errorCode, String errorArg)
	{
		String[] args = errorArg != null ? new String[] { errorArg } : null;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, field, errorCode, args, MessageCodeConstant.MSJ_ERROR_DEFAULT);
	}
}
